package com.sgcc.statistics.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.common.collect.Lists;
import com.sgcc.common.core.domain.R;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.statistics.domain.OBusiScale;
import com.sgcc.statistics.domain.ODigitalScale;
import com.sgcc.statistics.service.IOBusiScaleService;
import com.sgcc.statistics.service.IODigitalScaleService;
import com.sgcc.statistics.utils.PercentUtils;
import com.sgcc.system.api.RemoteDeptService;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.domain.SysDept;
import com.sgcc.system.api.domain.SysDictData;
import org.activiti.engine.impl.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.statistics.mapper.OServiceIndexMapper;
import com.sgcc.statistics.domain.OServiceIndex;
import com.sgcc.statistics.service.IOServiceIndexService;

/**
 * 服务成效指标维护Service业务层处理
 *
 * @author sgcc
 * @date 2021-03-18
 */
@Service
public class OServiceIndexServiceImpl implements IOServiceIndexService {
    @Autowired
    private OServiceIndexMapper oServiceIndexMapper;
    @Autowired
    private IODigitalScaleService oDigitalScaleService;
    @Autowired
    private IOBusiScaleService oBusiScaleService;
    @Autowired
    private RemoteDictDataService remoteDictDataService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询服务成效指标维护
     *
     * @param id 服务成效指标维护ID
     * @return 服务成效指标维护
     */
    @Override
    public OServiceIndex selectOServiceIndexById(String code) {
        return oServiceIndexMapper.selectOServiceIndexById(code);
    }

    /**
     * 查询服务成效指标维护列表
     *
     * @param oServiceIndex 服务成效指标维护
     * @return 服务成效指标维护
     */
    @Override
    public List<OServiceIndex> selectOServiceIndexList(OServiceIndex oServiceIndex) {
        return oServiceIndexMapper.selectOServiceIndexList(oServiceIndex);
    }

    /**
     * 新增服务成效指标维护
     *
     * @param oServiceIndex 服务成效指标维护
     * @return 结果
     */
    @Override
    public int insertOServiceIndex(OServiceIndex oServiceIndex) {
        return oServiceIndexMapper.insertOServiceIndex(oServiceIndex);
    }

    /**
     * 修改服务成效指标维护
     *
     * @param oServiceIndex 服务成效指标维护
     * @return 结果
     */
    @Override
    public int updateOServiceIndex(OServiceIndex oServiceIndex) {
        return oServiceIndexMapper.updateOServiceIndex(oServiceIndex);
    }

    /**
     * 批量删除服务成效指标维护
     *
     * @param ids 需要删除的服务成效指标维护ID
     * @return 结果
     */
    @Override
    public int deleteOServiceIndexByIds(String[] ids) {
        return oServiceIndexMapper.deleteOServiceIndexByIds(ids);
    }

    /**
     * 删除服务成效指标维护信息
     *
     * @param id 服务成效指标维护ID
     * @return 结果
     */
    @Override
    public int deleteOServiceIndexById(String id) {
        return oServiceIndexMapper.deleteOServiceIndexById(id);
    }

    @Override
    public List<OServiceIndex> selectOServiceIndexInfoList(OServiceIndex oServiceIndex) {
        List<OServiceIndex> re =new ArrayList<>();
//        oServiceIndex.setAuthNo(UserUtils.getUser().getCompany().getCode());
//		if(!oServiceIndex.isCounty() && oServiceIndex.getAuthNo()!=null && oServiceIndex.getAuthNo().length()>5){
//			return PageUtils.getEmpty();
//		}
//		if(oServiceIndex.getOrgNo() != null && oServiceIndex.getOrgNo().equals("35101")){
//			//查全部市
//			oServiceIndex.setOrgNo("");
//		}

        List<String> orgNos = getOrgNos(oServiceIndex.getDeptNo(), oServiceIndex.isCounty());
        if (CollectionUtil.isEmpty(orgNos)) {
            return re;
        }
        //月活跃指标、用户留存指标、用户实名认证指标
        ODigitalScale oDigitalScale = new ODigitalScale();
        BeanUtils.copyProperties(oServiceIndex, oDigitalScale);
        oDigitalScale.setOrgNo(oServiceIndex.getDeptNo());
        oDigitalScale.setList(orgNos);
        List<ODigitalScale> oDigitalScaleList = oDigitalScaleService.selectODigitalScaleList(oDigitalScale);
        //线上电费交费指标、自有渠道线上交费指标、第三方渠道线上交费指标、线上办电指标、高低压线上办理指标、光伏线上办理指标、充电桩线上办理指标、客户服务信息线上触达指标、线上服务满意指标、积分消费指标
        OBusiScale oBusiScale = new OBusiScale();
        BeanUtils.copyProperties(oServiceIndex, oBusiScale);
        oBusiScale.setOrgNo(oServiceIndex.getDeptNo());
        oBusiScale.setList(orgNos);
        List<OBusiScale> oBusiScaleList = oBusiScaleService.selectOBusiScaleList(oBusiScale);
        //全部指标参数
        List<SysDictData> typeCodes = null;

        R<List<SysDictData>> r = remoteDictDataService.selectDictDataByType("service_index_type");

        typeCodes=r.getData();

        OServiceIndex index1 = oServiceIndexMapper.selectByCode("0102");
        OServiceIndex index2 = oServiceIndexMapper.selectByCode("0101");
        OServiceIndex index3 = oServiceIndexMapper.selectByCode("0103");
        OServiceIndex index4 = oServiceIndexMapper.selectByCode("0201");
        OServiceIndex index5 = oServiceIndexMapper.selectByCode("020101");
        OServiceIndex index6 = oServiceIndexMapper.selectByCode("020102");
        OServiceIndex index7 = oServiceIndexMapper.selectByCode("0202");
        OServiceIndex index8 = oServiceIndexMapper.selectByCode("020201");
        OServiceIndex index9 = oServiceIndexMapper.selectByCode("020202");
        OServiceIndex index10 = oServiceIndexMapper.selectByCode("020203");
        OServiceIndex index11 = oServiceIndexMapper.selectByCode("0203");
        OServiceIndex index12 = oServiceIndexMapper.selectByCode("0204");
        OServiceIndex index13 = oServiceIndexMapper.selectByCode("0205");
        OServiceIndex index14 = oServiceIndexMapper.selectByCode("020501");
        List<OServiceIndex> resultList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(oDigitalScaleList)) {
            for (ODigitalScale digitalScale : oDigitalScaleList) {
                //月活跃指标
                OServiceIndex oServiceIndex1 = setIndex(digitalScale, index1);
                oServiceIndex1.setRealValueStr(digitalScale.getActiveRateStr());
                BigDecimal activeRate = digitalScale.getActiveRate();
                if (activeRate != null && activeRate.compareTo(index1.getTarget()) > -1) {
                    oServiceIndex1.setReach(true);
                } else {
                    oServiceIndex1.setReach(false);
                }
                resultList.add(oServiceIndex1);
                //用户留存指标
                OServiceIndex oServiceIndex2 = setIndex(digitalScale, index2);
                oServiceIndex2.setRealValueStr(digitalScale.getKeepRateStr());
                BigDecimal keepRate = digitalScale.getKeepRate();
                if (keepRate != null && keepRate.compareTo(index2.getTarget()) > -1) {
                    oServiceIndex2.setReach(true);
                } else {
                    oServiceIndex2.setReach(false);
                }
                resultList.add(oServiceIndex2);
                //用户实名认证指标
                OServiceIndex oServiceIndex3 = setIndex(digitalScale, index3);
                oServiceIndex3.setRealValueStr(digitalScale.getCertRateStr());
                BigDecimal certRate = digitalScale.getCertRate();
                if (certRate != null && certRate.compareTo(index2.getTarget()) > -1) {
                    oServiceIndex3.setReach(true);
                } else {
                    oServiceIndex3.setReach(false);
                }
                resultList.add(oServiceIndex3);
            }
        }
        if (CollectionUtil.isNotEmpty(oBusiScaleList)) {
            for (OBusiScale busiScale : oBusiScaleList) {
                //线上电费交费指标
                OServiceIndex oServiceIndex4 = setIndex2(busiScale, index4);
                BigDecimal rateComp4 = busiScale.getOnlinePayCompRate();
                BigDecimal target4 = index4.getTarget();
                if (target4 != null && rateComp4 != null) {
                    BigDecimal rate4 = rateComp4.multiply(target4);
                    oServiceIndex4.setRealValueStr(PercentUtils.getPercentNum(rate4));
                    if (rate4 != null && rate4.compareTo(index4.getTarget()) > -1) {
                        oServiceIndex4.setReach(true);
                    } else {
                        oServiceIndex4.setReach(false);
                    }
                } else {
                    oServiceIndex4.setReach(false);
                }
                resultList.add(oServiceIndex4);
                //自有渠道线上交费指标
                OServiceIndex oServiceIndex5 = setIndex2(busiScale, index5);
                oServiceIndex5.setRealValueStr(busiScale.getOwnChannelPayRateStr());
                BigDecimal rate5 = busiScale.getOwnChannelPayRate();
                if (rate5 != null && rate5.compareTo(index5.getTarget()) > -1) {
                    oServiceIndex5.setReach(true);
                } else {
                    oServiceIndex5.setReach(false);
                }
                resultList.add(oServiceIndex5);
                //第三方渠道线上交费指标
                OServiceIndex oServiceIndex6 = setIndex2(busiScale, index6);
                oServiceIndex6.setRealValueStr(busiScale.getOtherChannelPayRateStr());
                BigDecimal rate6 = busiScale.getOtherChannelPayRate();
                if (rate6 != null && rate6.compareTo(index6.getTarget()) > -1) {
                    oServiceIndex6.setReach(true);
                } else {
                    oServiceIndex6.setReach(false);
                }
                resultList.add(oServiceIndex6);
                //线上办电指标
                OServiceIndex oServiceIndex7 = setIndex2(busiScale, index7);
                BigDecimal rateComp7 = busiScale.getOnlineDealCompRate();
                BigDecimal target7 = index7.getTarget();
                if (target7 != null && rateComp7 != null) {
                    BigDecimal rate7 = rateComp7.multiply(target7);
                    oServiceIndex7.setRealValueStr(PercentUtils.getPercentNum(rate7));
                    if (rate7 != null && rate7.compareTo(index7.getTarget()) > -1) {
                        oServiceIndex7.setReach(true);
                    } else {
                        oServiceIndex7.setReach(false);
                    }
                } else {
                    oServiceIndex7.setReach(false);
                }
                resultList.add(oServiceIndex7);
                //高低压线上办理指标
                OServiceIndex oServiceIndex8 = setIndex2(busiScale, index8);
                oServiceIndex8.setRealValueStr(PercentUtils.getPercentNum(busiScale.getHighLowDealRate()));
                BigDecimal rate8 = busiScale.getHighLowDealRate();
                if (rate8 != null && rate8.compareTo(index8.getTarget()) > -1) {
                    oServiceIndex8.setReach(true);
                } else {
                    oServiceIndex8.setReach(false);
                }
                resultList.add(oServiceIndex8);
                //充电桩线上办理指标
                OServiceIndex oServiceIndex9 = setIndex2(busiScale, index9);
                oServiceIndex9.setRealValueStr(PercentUtils.getPercentNum(busiScale.getChargDealRate()));
                BigDecimal rate9 = busiScale.getChargDealRate();
                if (rate9 != null && rate9.compareTo(index9.getTarget()) > -1) {
                    oServiceIndex9.setReach(true);
                } else {
                    oServiceIndex9.setReach(false);
                }
                resultList.add(oServiceIndex9);
                //光伏线上办理指标
                OServiceIndex oServiceIndex10 = setIndex2(busiScale, index10);
                oServiceIndex10.setRealValueStr(PercentUtils.getPercentNum(busiScale.getPvoltDealRate()));
                BigDecimal rate10 = busiScale.getPvoltDealRate();
                if (rate10 != null && rate10.compareTo(index10.getTarget()) > -1) {
                    oServiceIndex10.setReach(true);
                } else {
                    oServiceIndex10.setReach(false);
                }
                resultList.add(oServiceIndex10);
                //客户服务信息线上触达指标
                OServiceIndex oServiceIndex11 = setIndex2(busiScale, index11);
                oServiceIndex11.setRealValueStr(PercentUtils.getPercentNum(busiScale.getReachRate()));
                BigDecimal rate11 = busiScale.getReachRate();
                if (rate11 != null && rate11.compareTo(index11.getTarget()) > -1) {
                    oServiceIndex11.setReach(true);
                } else {
                    oServiceIndex11.setReach(false);
                }
                resultList.add(oServiceIndex11);
                //线上服务满意指标
                OServiceIndex oServiceIndex12 = setIndex2(busiScale, index12);
                oServiceIndex12.setRealValueStr(PercentUtils.getPercentNum(busiScale.getSatiRate()));
                BigDecimal rate12 = busiScale.getSatiRate();
                if (rate12 != null && rate12.compareTo(index12.getTarget()) > -1) {
                    oServiceIndex12.setReach(true);
                } else {
                    oServiceIndex12.setReach(false);
                }
                resultList.add(oServiceIndex12);
                //积分消费指标
                OServiceIndex oServiceIndex14 = setIndex2(busiScale, index14);
                oServiceIndex14.setRealValueStr(PercentUtils.getPercentNum(busiScale.getPointRate()));
                BigDecimal rate14 = busiScale.getPointRate();
                if (rate14 != null && rate14.compareTo(index14.getTarget()) > -1) {
                    oServiceIndex14.setReach(true);
                } else {
                    oServiceIndex14.setReach(false);
                }
                resultList.add(oServiceIndex14);
            }
        }
        List<OServiceIndex> trueList = Lists.newArrayList();
        List<OServiceIndex> falseList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(resultList)) {
            for (OServiceIndex index : resultList) {
                for (SysDictData value : typeCodes) {
                    if (index.getType() != null && index.getType().equals(value.getDictValue())) {
                        index.setType(value.getDictValue());
                    }
                }
                if (index.getReach()) {
                    trueList.add(index);
                } else {
                    falseList.add(index);
                }
            }
        }
        if (oServiceIndex.getReach() == null) {
            Collections.sort(resultList);
            return resultList;
        } else if (oServiceIndex.getReach() == true) {
            Collections.sort(trueList);
            return trueList;
        } else {
            Collections.sort(falseList);
            return falseList;
        }
    }

    private OServiceIndex setIndex(ODigitalScale digitalScale, OServiceIndex index) {
        OServiceIndex oServiceIndex = new OServiceIndex();
        oServiceIndex.setDeptName(digitalScale.getOrgName());
        oServiceIndex.setDeptNo(digitalScale.getOrgNo());
        oServiceIndex.setType(index.getType());
        oServiceIndex.setCode(index.getCode());
        oServiceIndex.setName(index.getName());
        oServiceIndex.setTargetStr(PercentUtils.getPercentNum(index.getTarget()));
        return oServiceIndex;
    }

    private OServiceIndex setIndex2(OBusiScale oBusiScale, OServiceIndex index) {
        OServiceIndex oServiceIndex = new OServiceIndex();
        oServiceIndex.setDeptName(oBusiScale.getOrgName());
        oServiceIndex.setDeptNo(oBusiScale.getOrgNo());
        oServiceIndex.setType(index.getType());
        oServiceIndex.setCode(index.getCode());
        oServiceIndex.setName(index.getName());
        oServiceIndex.setTargetStr(PercentUtils.getPercentNum(index.getTarget()));
        return oServiceIndex;
    }


    public List<String> getOrgNos(String orgNo,boolean county){
        String authNo = tokenService.getLoginUser().getSysUser().getDeptId();
        List<String> grades;
        if(county){
            grades = Lists.newArrayList("3","4","5");
        }else {
            grades = Lists.newArrayList("3");
        }
        List<SysDept> offices =  oBusiScaleService.selectAuthByTypeAndGrades(authNo,orgNo,grades,"01");
        List<String> orgNos = Lists.newArrayList();
        if(CollectionUtil.isNotEmpty(offices)){
            for (SysDept office:offices) {
                orgNos.add(office.getDeptId());
            }
        }
        return orgNos;
    }

}
